package com.southminority.ethnic.service.student.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southminority.ethnic.controller.student.vo.ActivityDetailVo;
import com.southminority.ethnic.controller.student.vo.ActivityPageVo;
import com.southminority.ethnic.pojo.StudentActivity;
import com.southminority.ethnic.service.student.StudentActivityService;
import com.southminority.ethnic.mapper.student.StudentActivityMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author SouthMinority
 * @description 针对表【student_activity(学生活动表)】的数据库操作Service实现
 * @createDate 2025-11-17
 */
@Service
public class StudentActivityServiceImpl extends ServiceImpl<StudentActivityMapper, StudentActivity>
    implements StudentActivityService {

    @Override
    public Boolean incrementViews(Long id) {
        StudentActivity activity = this.getById(id);
        if (activity != null) {
            activity.setViews(activity.getViews() == null ? 1 : activity.getViews() + 1);
            return this.updateById(activity);
        }
        return false;
    }

    @Override
    public Page<ActivityPageVo> getActivityPage(Integer pageNum, Integer pageSize, String keyword, Integer status) {
        Page<StudentActivity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<StudentActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentActivity::getIsDeleted, 0);
        
        // 状态筛选：如果status不为null，则按指定状态筛选；否则查询全部
        if (status != null) {
            queryWrapper.eq(StudentActivity::getStatus, status);
        }
        
        if (keyword != null && !keyword.trim().isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                .like(StudentActivity::getTitle, keyword)
                .or()
                .like(StudentActivity::getActivityType, keyword)
                .or()
                .like(StudentActivity::getOrganizer, keyword)
            );
        }
        
        queryWrapper.orderByDesc(StudentActivity::getActivityDate)
                   .orderByDesc(StudentActivity::getCreateTime);

        queryWrapper.select(
                StudentActivity::getId,
                StudentActivity::getTitle,
                StudentActivity::getActivityType,
                StudentActivity::getActivityDate,
                StudentActivity::getLocation,
                StudentActivity::getSummary,
                StudentActivity::getCoverImage,
                StudentActivity::getOrganizer,
                StudentActivity::getParticipantsCount,
                StudentActivity::getViews,
                StudentActivity::getStatus,
                StudentActivity::getCreateTime,
                StudentActivity::getUpdateTime
        );
        Page<StudentActivity> activityPage = this.page(page, queryWrapper);
        
        // 转换为VO，不包含完整内容
        Page<ActivityPageVo> voPage = new Page<>(activityPage.getCurrent(), activityPage.getSize(), activityPage.getTotal());
        voPage.setRecords(activityPage.getRecords().stream().map(activity -> {
            ActivityPageVo vo = new ActivityPageVo();
            BeanUtils.copyProperties(activity, vo);
            return vo;
        }).toList());
        
        return voPage;
    }

    @Override
    public ActivityDetailVo getActivityDetail(Long id) {
        StudentActivity activity = this.getById(id);
        if (activity == null || activity.getIsDeleted() == 1 || activity.getStatus() != 1) {
            return null;
        }
        
        ActivityDetailVo vo = new ActivityDetailVo();
        BeanUtils.copyProperties(activity, vo);
        
        // 增加浏览次数
        incrementViews(id);
        
        return vo;
    }
}
